var app=getApp();
var ux = require('../../../../lib/ux.js');
var WxParse = require('../../../../utils/wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {    
    redImg:'',
    tongyi:2,
    jlzc:false,
    dlxy:false,
    card:{},
    agentType:0,
    banrdId:null,
    userId:null,
    getAgentCard:{},
    torderUrl:"/api/memberAgent/agentTOrder.do",
    action:"/api/memberAgent/getBanrdAgentDetails.do",
    agentUrls: "/api/memberAgent/isApplyAgent.do",
	submit:"{{submit}}"
  },
  toShopDetail: function (event) {
    var dataset = event.currentTarget.dataset;
    var shopid = dataset.shopid;
    var price = dataset.price;
    wx.navigateTo({
      url: '/pages/member/tab1/shop/detail?prices=' + price + '&id=' + shopid,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.hideShareMenu();
    var scene = decodeURIComponent(options.scene)
    if (scene && scene != undefined && scene != "undefined") {
      var scenes = scene.split("_");
      this.setData({
        banrdId: scene
      })
     var $this = this;
     wx.login({
      success: function (res) {
        if (res.code) {
          var jscode = res.code;
          //发起网络请求
          ux.request("/api/member/isLogin.do", {
            jscode: jscode
          }, function (protocol) {
              wx.setStorageSync("token", protocol.data.token);
              wx.setStorageSync("userInfo", protocol.data.userInfo);
              wx.setStorageSync("setting", protocol.data.setting);
              $this.setData({
                userId: protocol.data.userInfo.id
              })
              var pp = { banrdId: $this.data.banrdId };
              ux.request($this.data.agentUrls, pp, function (protocol) {
                $this.setData({
                  agentType: protocol.data.agentType
                })
              }, true);
              var pamars = { banrdId: $this.data.banrdId}
              ux.request($this.data.action, pamars,function(protocol){
                  $this.setData({
                    lists: JSON.parse(protocol.data.card.products),
                    card: protocol.data.card
                  });
                  $this.getShar();
                WxParse.wxParse('articleDetail', 'html', protocol.data.card.agreement, $this);
                },true); 
          }, true);
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      },
    })
     
    }else{

      this.setData({
        redImg: wx.getStorageSync("parameter").zfFm,
        banrdId: options.banrdId ? options.banrdId : wx.getStorageSync("banrdId"),
        userId: wx.getStorageSync("userInfo").uid
      })
     var $this = this;
     var pp = { banrdId: this.data.banrdId };
     ux.request($this.data.agentUrls, pp, function (protocol) {
       $this.setData({
         agentType: protocol.data.agentType
       })
       console.log("代理卡" + protocol.data.agentType)
     }, true);
     var pamars = { banrdId: this.data.banrdId}
     ux.request(this.data.action, pamars,function(protocol){
         $this.setData({
           lists: JSON.parse(protocol.data.card.products),
           card: protocol.data.card
         });
         $this.getShar();
       WxParse.wxParse('articleDetail', 'html', protocol.data.card.agreement, $this);
       },true); 
    } 
  },
  getShar:function(){
    var self=this;
    var banrdid=wx.getStorageSync('banrdId');
    ux.request("/api/Merchant/getShars.do", {banrd:parseInt(banrdid),type:2},function(data){
      if(data.data.obj){
        self.setData({
          fenxiang:data.data.obj
        })
      }
    });
  },
  onForm:function(event){
	if(this.data.submit){
		  var $this = this;
		  var dataset = event.currentTarget.dataset;
		  ux.request(this.data.submit,detail.value,function(protocol){
				if(protocol.success){
					if(dataset.url){
						wx.redirectTo(dataset.url);
					}
				}
		  },true);
	 }
  },
  preventTouchMoveTy:function(){
    return false;
  },
  jlzcOpen: function () {
    this.setData({
      jlzc: true
    })
  },
  dlxyOpen: function () {
    this.setData({
      dlxy: true
    })
  },
  jlzcX:function(){
    this.setData({
      jlzc:false
    })
  },
  dlxyX: function () {
    this.setData({
      dlxy: false
    })
  },
  onTap:function(event){
	  var dataset = event.currentTarget.dataset;
	  var data = this.data;
	  if(dataset.key && dataset.val){
		  data[dataset.key] = dataset.val;
	  }
	  this.setData(data);
  },
  tongyis: function () {
    if (this.data.tongyi == 2) {
      this.setData({
        tongyi: 1
      })
    } else {
      this.setData({
        tongyi: 2
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  
  toPay: function (e){
    if(this.data.agentType==1){
      this.toast("您已是代理身份无需再次购买");
      return false;
    }
    if(this.data.tongyi==2){
      this.toast("请仔细阅读协议并同意");
      return false;
    }
      var self = this;
      wx.showLoading({
        mask: true,
        title: '处理中....'
      })
    var params = { formId: e.detail.formId, cardId: this.data.card.id };
      ux.request(this.data.torderUrl, params, function (data) {
          if (data.data.type==2) {
            var order = data.data;
            wx.requestPayment({
              timeStamp: order.timeStamp,
              nonceStr: order.nonceStr,
              package: order.package,
              signType: 'MD5',
              paySign: order.paySign,
              success: function (res) {
                wx.reLaunch({
                  url: '/pages/member/tab1/sugarSpice?id=' + self.data.banrdId,
                })
              },
              fail: function (res) {
                wx.showToast({
                  title: "支付失败",
                  duration: 3000
                })
                wx.hideLoading();
              }
            })
          } else if (data.data.type == 1 || data.data.type == 3){
            wx.hideLoading()
           self.toast(data.data.msg);
          }else{
            wx.hideLoading()
            self.toast("支付失败");
          }
        },true);
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      var self = this;
      self.setData({
        authModle: false
      })
      wx.showShareMenu({
        withShareTicket: true
      })
      var shareObj = {
        title: this.data.fenxiang.shareTitle,
        path: '/pages/member/tab1/agent/applyAgent?tab=0&userId=' + self.data.userId + "&banrdId=" + self.data.banrdId,
        imageUrl: this.data.fenxiang.url,
        success: function (res) {
        },
        fail: function () {
          wx.showToast({
            title: '转发失败',
          })
        }
      }
      return shareObj;
    } else {

    }
  },
})
